<template>
	<view class="uni_box home">
		<navbar title='发布名片'>
		</navbar>
		<view class="content">
			<view class="leiview" @click="mpshow=true">
				<view class="leiitem">
					<view class="shu"></view>
					<text class="name">名片类型<text class="xing">*</text></text>
					<view class="leiitem ml">
						<text class="xuan">{{ mpinfo.label?mpinfo.label:'请选择' }}</text>
						<image class="nav_icon_back" src="/static/img/nav_icon_back.png"></image>
					</view>
				</view>
			</view>
			<view class="leiview">
				<view class="leiitem oneitem">
					<view class="shu"></view>
					<text class="name">名片标题<text class="xing">*</text></text>
				</view>
				<view class="leiitem twoinput">
					<u-input v-model="mpfabu.title" type="text" :border="false" placeholder="请输入"/>
				</view>
			</view>
			<view class="leiview" v-if="id==''">
				<view class="leiitem">
					<view class="shu"></view>
					<text class="name">任务数量</text>
					<view class="leiitem ml">
						<u-number-box :min="30" v-model="mpfabu.num" @change="valChange"></u-number-box>
					</view>
				</view>
			</view>
			<view class="leiview">
				<view class="leiitem oneitem">
					<view class="shu"></view>
					<text class="name">审核时间<text class="xing">*</text></text>
				</view>
				<view class="leiitem twoinput">
					<u-input v-model="mpfabu.shenhe_min" type="number" :border="false" placeholder="请输入"/>
					<text class="yuan">分</text>
				</view>
			</view>
			<view class="leiview">
				<view class="leiitem oneitem">
					<view class="shu"></view>
					<text class="name">完成时间<text class="xing">*</text></text>
				</view>
				<view class="leiitem twoinput">
					<u-input v-model="mpfabu.end_min" type="number" :border="false" placeholder="请输入"/>
					<text class="yuan">分</text>
				</view>
			</view>
			
			<view class="leiview">
				<view class="leiitem oneitem">
					<view class="shu"></view>
					<text class="name">名片介绍</text>
				</view>
				<view class="leiitem twoinput">
					<u-input v-model="mpfabu.descript" maxlength="10000" type="textarea" :border="false" placeholder="请输入"/>
				</view>
			</view>
			<view class="leiview">
				<view class="leiitem oneitem">
					<view class="shu"></view>
					<text class="name">{{ mpinfo.label?mpinfo.label:'请选择类型' }}</text>
				</view>
				<view class="leiitem twoinput">
					<u-input v-model="mpfabu.qun"  maxlength="10000" type="textarea" :border="false" placeholder="请输入"/>
				</view>
			</view>
			<view class="leiview">
				<view class="leiitem">
					<view class="shu"></view>
					<text class="name">名片头像<text class="xing">*</text></text>
					<text class="zuiduo">最多上传1张</text>
				</view>
				<view class="leiitem col buviewtwo">
					<view class="tuview">
						<view class="tuitem" @click="upload(1)" v-if="mpfabu.image.length<1">
							<image class="addtu" src="/static/img/btn_tupian.png"></image>
						</view>
						<view class="tuitem" v-for="(item,index) in mpfabu.image" :key="index">
							<view class="cha" @click="del(1,index)">
								<image class="shanicon" src="/static/img/shanicon.png"></image>
							</view>
							<image class="addtu" :src="item"></image>
						</view>
					</view>
				</view>
			</view>
			<view class="leiview">
				<view class="leiitem">
					<view class="shu"></view>
					<text class="name">添加二维码图片<text class="xing">*</text></text>
					<text class="zuiduo">最多上传1张</text>
				</view>
				<view class="leiitem col buviewtwo">
					<view class="tuview">
						<view class="tuitem" @click="upload(2)" v-if="mpfabu.qrcode.length<1">
							<image class="addtu" src="/static/img/btn_tupian.png"></image>
						</view>
						<view class="tuitem"  v-for="(item,index) in mpfabu.qrcode" :key="index">
							<view class="cha"  @click="del(2,index)">
								<image class="shanicon" src="/static/img/shanicon.png"></image>
							</view>
							<image class="addtu" src="/static/img/btn_tupian.png"  :src="item"></image>
						</view>
					</view>
				</view>
			</view>
			<view class="leiview">
				<view class="leiitem">
					<view class="shu"></view>
					<text class="name">添加示例图<text class="xing">*</text></text>
					<text class="zuiduo">最多上传3张</text>
				</view>
				<view class="leiitem col buviewtwo">
					<view class="tuview">
						<view class="tuitem"  @click="upload(3)" v-if="mpfabu.images.length<3">
							<image class="addtu" src="/static/img/btn_tupian.png"></image>
						</view>
						<view class="tuitem"  v-for="(item,index) in mpfabu.images" :key="index">
							<view class="cha"  @click="del(3,index)">
								<image class="shanicon" src="/static/img/shanicon.png"></image>
							</view>
							<image class="addtu"  :src="item"></image>
						</view>
					</view>
				</view>
			</view>
		</view>

		<view class="botview" v-if="id==''">
			<text class="jinprice">支付金额：<text class="jinnum">{{ price }}</text></text>
			<view class="lianxi" :style="{
				background: zhucolor
			}" @click="shenqing">
				立即发布
			</view>
		</view>
		<view class="botview" v-if="id!=''">
			<view class="lianxi" :style="{
				background: zhucolor
			}" @click="shenqing">
				确定修改
			</view>
		</view>

		<!-- 时间选择 -->
		<u-calendar v-model="rilishow" :mode="mode" @change="richange"></u-calendar>

		<!-- 名片类型 -->
		<u-select v-model="mpshow" :list="mplist" @confirm="mpconfirm"></u-select>
	</view>
</template>

<script>
import {
		baseUrl
	} from '@/utils/config.js';
	export default {
		data() {
			return {
				phone:"",
				value: 0,
				mode:'date',
				rilishow:false,
				zhucolor: uni.getStorageSync('zhu_color'),
				mplist:[],//名片类型
				mpinfo:{},//名片类型选择
				mpshow:false,
				mplist:[
					// {
					// 	value: '1',
					// 	label: '江'
					// },
					// {
					// 	value: '2',
					// 	label: '湖'
					// }
				],
				mpfabu:{
					title:'',
					cid:'',
					descript:'',//简介
					qun:'',//群
					image:[],//名片头像
					qrcode:[],//二维码
					images:[],//示例图
					num:30,//任务数量
					shenhe_min:'',//审核时间
					end_min:'',//完成时间
					price:0

				},
				updatetype:1,//1头像 2二维码 3示例图
				headimg:'',
				price:0,
				danprice:0,
				info:{},

				id:'',
				type:'card',
				newinfo:{},
				pyq_price:0,
				oldprice:0
			}
		},
		onLoad(option) {
			this.fcardgetInit();//名片配置
			this.getcardgetCate();//名片类型
			this.getuserinfo();
			if(option.id){
				this.id = option.id
				
			}
		},
		onShow() {
		},
		methods: {
			async getuserinfo(){
				let res = await this.$u.api.usergetData({
				});
				this.info = res
				// this.user = res.user
			},
			async fcardgetInit(){
				let res = await this.$u.api.cardgetInit({
				});
				this.danprice = res.price
				this.price = res.price
				this.mpfabu.price = res.price
				this.pyq_price = res.pyq_price
				this.oldprice = res.price
			},

			async huixian(){
				let res = await this.$u.api.getEdit({
					id:this.id,
					type:this.type
				});
				this.newinfo = res

				//回显
				let mplist = this.mplist
				for(let i in mplist){
					if(mplist[i].value == this.newinfo.cid){
						this.mpinfo.label = mplist[i].label
						this.mpinfo.value = mplist[i].value
					}
				}
				console.log(this.mpinfo.label)
				this.mpfabu.cid = this.newinfo.cid
				this.mpfabu.title = this.newinfo.title
				
				this.mpfabu.num = this.newinfo.nums||30

				// this.mpfabu.num = this.newinfo.num
				this.mpfabu.shenhe_min = this.newinfo.shenhe_min
				this.mpfabu.end_min = this.newinfo.end_min
				this.mpfabu.descript = this.newinfo.descript
				this.mpfabu.qun = this.newinfo.qun
				if(this.newinfo.image){
					let arrimage = this.newinfo.image.split(",");
					for(let i in arrimage){//this.newinfo.cdnurl+
						arrimage[i] = arrimage[i]
					}
					this.mpfabu.image = arrimage
				}
				if(this.newinfo.qrcode){
					let arrimageqrcode = this.newinfo.qrcode.split(",");
					for(let i in arrimageqrcode){
						arrimageqrcode[i] = arrimageqrcode[i]
					}
					this.mpfabu.qrcode = arrimageqrcode
				}
				if(this.newinfo.images){
					let arrimageimages = this.newinfo.images.split(",");
					for(let i in arrimageimages){
						arrimageimages[i] = arrimageimages[i]
					}
					this.mpfabu.images = arrimageimages
				}
				console.log(this.mpfabu)

				
				this.$forceUpdate()
			},
			async getcardgetCate(){
				let res = await this.$u.api.cardgetCate({
				});
				let shuzu = res
				for(let i in shuzu){
					shuzu[i].label = shuzu[i].name
					shuzu[i].value = shuzu[i].id
				}
				this.mplist = shuzu
				if(this.id){
					this.huixian();//回显数据
				}
			},
			valChange(e) {

				console.log('当前值为: ' + e.value)
				this.gettotal()
			},
			gettotal(){
				//计算价格
				this.price = (Number(this.mpfabu.num)*Number(this.danprice)).toFixed(2)
				this.mpfabu.price = this.price
			},
			richange(e){
				console.log(e)
			},
			mpconfirm(e){
				console.log(e)
				this.mpinfo.label = e[0].label
				this.mpinfo.value = e[0].value
				this.mpfabu.cid = e[0].value
				if(e[0].value == 16){
					//朋友圈任务
					this.danprice = this.pyq_price
					this.price = this.pyq_price
					this.mpfabu.price = this.pyq_price
				}else{
					this.danprice = this.oldprice
					this.price = this.oldprice
					this.mpfabu.price = this.oldprice
				}
				this.gettotal()
			},
			upload(e) {
				this.updatetype = e
				uni.chooseImage({
					count: 1,
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						this.headimg = tempFilePaths[0];
						// this.isClick = true;
						this.unloadtou()
						// this.getavatar();//修改信息
					}
				});
			},
			unloadtou(){
				// console.log(this.head_portrait)
				let that = this
				uni.uploadFile({
					// url: baseUrl + '/api/uploadImage', //仅为示例，非真实的接口地址
					url: baseUrl + '/common/upload', //仅为示例，非真实的接口地址
					filePath: this.headimg,
					name: 'file',
					header: {
						// 'content-type': 'application/json;charset=UTF-8',
						'token':uni.getStorageSync('token'),
					},
					formData: {
						'token':uni.getStorageSync('token'),
					},
					success: (uploadFileRes) => {
						// console.log( JSON.parse(uploadFileRes.data).data);
						let mpfabu = that.mpfabu
						if(that.updatetype==1){
							//名片
							mpfabu.image.push(JSON.parse(uploadFileRes.data).data.fullurl)
						}else if(that.updatetype==2){
							//二维码
							mpfabu.qrcode.push(JSON.parse(uploadFileRes.data).data.fullurl)
						}else if(that.updatetype==3){
							//示例图
							mpfabu.images.push(JSON.parse(uploadFileRes.data).data.fullurl)
						}
					}
				});
			},
			del(type,index){
				let mpfabu = this.mpfabu
				if(type == 1){
					mpfabu.image.splice(index,1)
				}else if(type ==2){
					mpfabu.qrcode.splice(index,1)
				}else if(type ==3){
					mpfabu.images.splice(index,1)
				}
				this.mpfabu = mpfabu
			},
			async shenqing(){
				console.log(this.mpfabu)
				// let parms = this.mpfabu
				let parms = JSON.parse(JSON.stringify(this.mpfabu))
				parms.image = parms.image.toString()
				parms.images = parms.images.toString()
				parms.qrcode = parms.qrcode.toString()
				if(this.id != ''){
					parms.id = this.id
					parms.type = this.type
				}
				// if(this.id == ''){
				// 	if(Number(this.price)>Number(this.info.money)){
				// 		this.$u.toast("余额不足 请先进行充值");
				// 		setTimeout(() => {
				// 			this.navrouter("/pages_subject/twoPage/Chuzhi");
				// 		}, 1000);
				// 		return false;
				// 	}
				// }
				if(this.id == ''){
					let res = await this.$u.api.cardadd(parms);
					this.$u.toast("操作成功");
					setTimeout(() => {
						uni.navigateBack()
					}, 1000);
				}else{
					let res = await this.$u.api.doEdit(parms);
					this.$u.toast("编辑成功");
					setTimeout(() => {
						uni.navigateBack()
					}, 1000);
				}
				
			}
		}
	}
</script>

<style lang="scss" scoped>
	page{
		background-color: #fff;
	}
	.content{
		min-height: 100vh;
		background-color: #f5f5f5;
		padding: 20rpx 0 150rpx;
		.leiview{
			margin-bottom: 20rpx;
			display: flex;
			flex-direction: column;
			background-color: #fff;
			padding:25rpx 30rpx;
			.yuan{
				font-family: PingFang SC;
				font-weight: 500;
				font-size: 30rpx;
				color: #333333;
				display: inline-block;
				margin-left: 30rpx;
			}

			.leiitem{
				display: flex;
				flex-direction: row;
				align-items: center;
		        .name{
					font-family: PingFang SC;
					font-weight: bold;
					font-size: 30rpx;
					color: #444444;
				}
			}
			.xing{
				font-weight: bold;
				font-size: 30rpx;
				color: #FB1515;
				display: inline-block;
				margin-left: 6rpx;
			}
			.col{
				display: flex;
				flex-direction: column;
				align-items: flex-start;

			}
			.buzhou{
				font-weight: bold;
				font-size: 28rpx;
				color: #444444;
			}
			.buview{
				margin-top: 20rpx;
				border-bottom: 1rpx solid #eee;
				margin-bottom: 20rpx;
				padding-bottom: 15rpx;
			}
			.buviewtwo{
				margin-top: 20rpx;
				// border-bottom: 1rpx solid #eee;
				margin-bottom: 20rpx;
				// padding-bottom: 15rpx;
			}
			.buview:last-child{
				// border-bottom: none;
			}
			.zuiduo{
				font-weight: 500;
				font-size: 24rpx;
				color: #999999;
				display: inline-block;
				margin-left: 10rpx;
			}
			
			.tuview{
				display: flex;
				flex-direction: row;
				flex-wrap: wrap;
				// margin-top: 30rpx;
				.tuitem{
					width: 200rpx;
					height: 200rpx;
					position: relative;
					border-radius: 10rpx;
					margin: 0 20rpx 20rpx 0;
					.addtu{
						position: absolute;
						width: 100%;
						height: 100%;
						border-radius: 10rpx;
					}
					.cha{
						background-color: rgba(0, 0, 0, 0.4);
						position: absolute;
						width: 100%;
						height: 100%;
						display: flex;
						align-items: center;
						justify-content: center;
						border-radius: 10rpx;
						z-index: 10;
						text{
							font-family: PingFang SC;
							font-weight: 500;
							font-size: 24rpx;
							color: #FFFFFF;
						}
						.shanicon{
							position: relative;
							width: 50rpx;
							height: 50rpx;
							// right: 20rpx;
							// top: 20rpx;
						}
					}
					
				}
			}

			.wenben{
				background: #F5F5F5;
				border-radius: 20rpx;
				padding: 30rpx;
				width: 100%;
				margin: 20rpx 0;
			}
			.renview{
				display: flex;
				flex-direction: column;
				.renitem{
					display: flex;
					flex-direction: row;
					align-items: center;
					flex-wrap: wrap;
					.biaoqian{
						padding: 13rpx 19rpx;
						background: #EEEEEE;
						border-radius: 10rpx;
						display: flex;
						flex-direction: row;
						align-items: center;
						margin: 0 15rpx 15rpx 0;
						.facha{
							width: 24rpx;
							height: 24rpx;
							margin-left: 13rpx;
						}
						.biaoname{
							font-weight: 500;
							font-size: 28rpx;
							color: #666666;
						}
					}
				}
				.fajia{
					width: 116rpx;
					height: 52rpx;
					margin-top: 20rpx;
				}
			}
			.oneitem{
				margin-bottom: 20rpx;
			}
			.shu{
				display: inline-block;
				width: 8rpx;
				height: 30rpx;
				background: linear-gradient(90deg, #0DBFFB, #198EFF);
				margin-right: 7rpx;
			}
			.twoinput{
				background: #F8F8F8;
				border-radius: 6rpx;
				padding: 10rpx 20rpx;
			}
			.xuan{
				font-weight: bold;
				font-size: 30rpx;
				color: #0DBEFB;
			}
			.nav_icon_back{
				margin-left: 20rpx;
				width: 24rpx;
				height: 24rpx;
			}
			.addbuzhou{
				display: flex;
				align-items: center;
				justify-content: center;
				padding: 10rpx 20rpx;
				border-radius: 10rpx;
				border: 2rpx solid #EBEBEB;
				margin-left: auto;
				font-weight: 500;
				font-size: 30rpx;
				color: #444444;
			}
		}
	}
	.ml{
		margin-left: auto;
	}

	.botview {
	height: 120rpx;
	background: #FFFFFF;
	width: 100%;
	position: fixed;
	bottom: 0;
	display: flex;
	flex-direction: row;
	align-items: center;
	z-index: 10;
	border-top: 1rpx solid #eee;
	padding: 30rpx;


	.botleft {
		width: 20%;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;

		.icon_fenxiang {
			width: 44rpx;
			height: 44rpx;

		}

		text {
			font-size: 18rpx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #333333;
		}
	}

	.jinprice{
		font-weight: bold;
		font-size: 30rpx;
		color: #444444;
	}
	.jinnum{
		color: #F11B1B;
		font-size: 35rpx;
	}
	.lianxi {
		margin-left: auto;
		width: 240rpx;
		height: 88rpx;
		background: linear-gradient(-82deg, #4BB6F0, #1D8DFD);
		border-radius: 44rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-weight: 500;
		font-size: 28rpx;
		color: #FFFFFF;
	}
}
	
</style>
